
export class Utils {

//ok
    decToBCD(data, length) {
        let array = new Uint8Array(new ArrayBuffer(length));
        let longArray = new Uint32Array(new ArrayBuffer(4));
        for (let i = length - 1; i >= 0; i--) {
            longArray[0] = data % 100;
            array[i] = (((longArray[0] / 10) << 4) + ((longArray[0] % 10) & 0x0F));
            data /= 100;
        }
        return array;
    }

//?
    long2Char(src, srcLength, longValue, dstOffset) {
        let array = new Uint8Array(new ArrayBuffer(8));
        for (let i = 0; i < 8; i++) {
            array[i] = this.unsignedRsh(longValue,8 * (7 - i));
        }
        this.charArrayCopy(array, 0, src, dstOffset, 8);
    }

    char2Long(src, srcLength, srcOffset, disOffset) {
        let array = new Uint8Array(new ArrayBuffer(8));
        this.charArrayCopy(src, srcOffset, array, disOffset, srcLength);

        let longArray = new Uint32Array(new ArrayBuffer(4 * 8));
        for (let i = 0; i < 8; i++) {
            longArray[i] = (array[i] & 0xff) << (8 * (7 - i));
        }

        console.log("char2Long", longArray);

        let intValue = new Uint32Array(new ArrayBuffer(4));
        for (let i = 0; i < 8; i++) {
            intValue[0] |= longArray[i];
        }
        return intValue[0];
    }

    str2Bcd(src, srcLength, dst) {
        if (srcLength % 2 !== 0) {
            srcLength += 1;
        }

        let j, k, p;

        for (p = 0; p < srcLength / 2; p++) {
            if ((src[2 * p] >= '0') && (src[2 * p] <= '9')) {
                j = src[2 * p] - '0';
            } else if ((src[2 * p] >= 'a') && (src[2 * p] <= 'z')) {
                j = src[2 * p] - 'a' + 0x0a;
            } else {
                j = src[2 * p] - 'A' + 0x0a;
            }

            if ((src[2 * p + 1] >= '0') && (src[2 * p + 1] <= '9')) {
                k = src[2 * p + 1] - '0';
            } else if ((src[2 * p + 1] >= 'a') && (src[2 * p + 1] <= 'z')) {
                k = src[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = src[2 * p + 1] - 'A' + 0x0a;
            }

            dst[p] = (j << 4) + k;
        }
    }

    char2Int(src, srcLength, srcOffset) {
        let array = new Uint8Array(new ArrayBuffer(2));
        this.charArrayCopy(src, srcOffset, array, 0, 2);

        let intValue = new Uint32Array(new ArrayBuffer(4));
        for (let i = 0; i < array.length; i++) {
            intValue[0] += (array[i] & 0xff) << (8 * (1 - i));
        }
        return intValue[0];
    }

    charArrayCopy(src, srcOffset, dst, dstOffset, count) {
        for (let i = 0; i < count; i++) {
            dst[dstOffset + i] = src[srcOffset + i];
        }
    }


    //向右位移
    rsh(value, n) {
        let bytes = value.toString(2);
        if (bytes.length <= n) {
            return 0;
        } else {
            let newBytes = bytes.substr(0, bytes.length - n);
            return  parseInt(newBytes, 2);
        }
    }

    //无符号向右位移
    unsignedRsh(value, n) {
        let bytes = value.toString(2);
        if (bytes.length <= n) {
            return 0;
        } else {
            let newBytes = bytes.substr(0, bytes.length - n);
            var uint8Value = new Uint8Array(1);
            uint8Value[0] = parseInt(newBytes, 2);
            return uint8Value[0];
        }
    }

    //左位移
    lsh(value,n) {
        let bytes = value.toString(2);
        if (bytes.length <= n) {
            return 0;
        } else {
            let newBytes = bytes.substr(n);
            for (let i = 0; i < n; i++) {
                newBytes += "0";
            }
            let result = parseInt(newBytes, 2);
            return result;
        }
    }

    //无符号向左位移
    unsignedLsh(value,n) {
        let bytes = value.toString(2);
        if (bytes.length <= n) {
            return 0;
        } else {
            var uint8Value = new Uint8Array(1);
            let newBytes = bytes.substr(n);
            for (let i = 0; i < n; i++) {
                newBytes += "0";
            }
            uint8Value[0] = parseInt(newBytes, 2);
            return uint8Value[0];
        }
    }
}

const utils = new Utils();
export default utils
